package com.ws.boot.demo.validate.service.manual.impl;

import com.ws.boot.demo.validate.entity.*;
import com.ws.boot.demo.validate.service.manual.ManualService;
import com.ws.boot.validate.context.ConstraintValidatorContext;
import com.ws.boot.validate.exception.ValidateException;
import com.ws.boot.validate.processor.ValidatorProcessor;
import com.ws.boot.web.domain.DefaultResponse;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 手动校验 实现类 .
 *
 * @className: ManualServiceImpl
 * @author: ws
 * @date: 2020/4/7 10:50
 * @version: 1.0.0
 */
@Service
public class ManualServiceImpl implements ManualService {

    /**
     * 校验没有 @ValidBean .
     *
     * @param idCard2
     * @return DefaultResponse
     * @author ws
     * @date 2020/4/7 11:43
     **/
    @Override
    public DefaultResponse checkerNoValidBean(IdCard2 idCard2) {
        List<ConstraintValidatorContext> violations = validatorProcessor.validateForManual(idCard2);
        throwValidationException(violations);
        return DefaultResponse.success();
    }

    /**
     * 校验 Bean 一层 .
     *
     * @param idCard
     * @return DefaultResponse
     * @author ws
     * @date 2020/4/7 11:44
     **/
    @Override
    public DefaultResponse checkerBeanOneLevel(IdCard idCard) {
        List<ConstraintValidatorContext> violations = validatorProcessor.validateForManual(idCard);
        throwValidationException(violations);
        return DefaultResponse.success();
    }

    /**
     * 校验 Bean 两层 .
     *
     * @param person
     * @return DefaultResponse
     * @author ws
     * @date 2020/4/7 10:49
     **/
    @Override
    public DefaultResponse checkerBeanTwoLevel(Person person) {
        List<ConstraintValidatorContext> violations = validatorProcessor.validateForManual(person);
        throwValidationException(violations);
        return DefaultResponse.success();
    }

    /**
     * 校验 Bean 三层 .
     *
     * @param person4
     * @return DefaultResponse
     * @author ws
     * @date 2020/4/15 10:46
     **/
    @Override
    public DefaultResponse checkerBeanThreeLevel(Person4 person4) {
        List<ConstraintValidatorContext> violations = validatorProcessor.validateForManual(person4);
        throwValidationException(violations);
        return DefaultResponse.success();
    }

    /**
     * 校验 Bean 带分组 三层 .
     *
     * @param person5
     * @param groups
     * @return DefaultResponse
     * @author ws
     * @date 2020/4/15 11:38
     **/
    @Override
    public DefaultResponse checkerBeanWithGroupThreeLevel(Person5 person5, Class... groups) {
        List<ConstraintValidatorContext> violations = validatorProcessor.validateForManual(person5, groups);
        throwValidationException(violations);
        return DefaultResponse.success();
    }

    /**
     * 抛校验异常 .
     * @param violations
     * @author ws
     * @date 2020/4/7 11:21
     * @return void
     **/
    private void throwValidationException(List<ConstraintValidatorContext> violations) {
        if (CollectionUtils.isEmpty(violations)) {
            return;
        }
        String message = violations.stream()
                .map(ConstraintValidatorContext::getMessage)
                .collect(Collectors.joining(";\r\n"));
        throw new ValidateException(message);
    }

    @Autowired
    private ValidatorProcessor validatorProcessor;
}
